package primary;

import interfaces.DALException;

import java.util.ArrayList;
import java.util.Scanner;

import dto_objects.BookDTO;

public class FrontEnd {
	
	private Scanner input = new Scanner(System.in);
	private Function function;
	

	public FrontEnd(Function function) {
		this.function = function;
	}

	public void start() {		
		System.out.print("Username:");
		String username = input.nextLine();
		System.out.print("Password:");
		String password = input.nextLine();
		
		if(function.validateLogin(username, password)){
			loggedIn();
		}else{
			System.out.println("Invalid username or password");
			System.out.println();
			start();
		}
	}
	
	private void loggedIn(){
		System.out.println("Logged in!");
		menu();
	}
	
	private void menu(){
		System.out.println();
		System.out.println("----------------------");
		System.out.println("Press 1 to lend books");
		System.out.println("Press 2 to return books");
		System.out.println("press 3 to logout");
		System.out.print("Input:");
		String choiceString = input.nextLine();
		
		try{
			int intChoice = Integer.parseInt(choiceString);  
			switch (intChoice) {
			case 1: firstBookInput("lend");	break;
			case 2: firstBookInput("return"); break;
			case 3: logout();break;

			default:
				System.out.println("Invalid input");
				System.out.println();
				menu();
				break;
			}
		}
		catch(NumberFormatException e){
			System.out.println("Invalid input");
			System.out.println();
			menu();
		}
	}

	private void logout() {
		System.out.println("Logged out!");
		System.out.println("----------------------");
		System.out.println();
		start();
	}
	
	private void firstBookInput(String lendReturn){
		System.out.println();
		System.out.println("----------------------");
		System.out.println("Use scanner to scan the barcode of the books you want to "+lendReturn);
		System.out.println("Press Enter when done");
		
		scanBooks(lendReturn);

		System.out.println(lendReturn+"ing done!");
		menu();
	}
	
	private void scanBooks(String lendReturn){
		ArrayList<BookDTO> bookList = new ArrayList<BookDTO>();
		
		while(true){
			System.out.print("Input:");
			String bookInputString = input.nextLine();
			if (bookInputString.equals("")){
				break;
			}
			
			try{
				int bookIDint = Integer.parseInt(bookInputString);
				
				for (BookDTO bookDTO : bookList) {
					if(bookDTO.getBookID() == bookIDint){
						throw new BookException("Book already scanned!");
					}
				}
				
				bookList.add(function.validateBook(bookIDint, lendReturn));
				
			} catch(NumberFormatException e){
				System.out.println("Invalid input");			
			} catch (DALException e) {
				System.out.println("Book does not exist contact librarian");
			} catch (BookException e) {
				System.out.println(e.getMessage());
			}	
		}
		
		if(bookList != null){
			bookList = function.updateDatabase(bookList, lendReturn); //Updates the statuses
		}
	}
}
